/*
    Watchpost - A file monitor program for the GNOME desktop
    Copyright (C) 2006 Andrew Resch

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include "config.h"
#include "watchpost.h"
#include "callbacks.h"
#include "interface.h"

/* Main program */
int main(int argc, char *argv[]) {
	/* initialize gtk and glade */
	gtk_init(&argc, &argv);
	glade_init();
	gnome_vfs_init();
	/* initialize gnome app */
	watchpost_gnome = gnome_program_init(PACKAGE, VERSION, LIBGNOMEUI_MODULE, argc, argv, GNOME_PARAM_APP_DATADIR, DATADIR, NULL);
	g_set_application_name("Watchpost");

	/* load and setup the interface */
	load_interface_files();
	setup_monitor_list_view();
	setup_results_list_view();
	setup_status_icon();
	
	/* connect all the signals to their callbacks */
	connect_callback_signals();

	/* initialize the monitor list hash table */
	monitor_list_hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);

	/* start the event loop */
	gtk_main();

	/* free the hash table */
	g_hash_table_destroy(monitor_list_hash_table);

	return 0;
}


/* Helper Functions */
void watchpost_toggle_visibility() {
	GtkWidget *main_window = glade_xml_get_widget(mainwindow_xml, "main_window");
	/* check to see if the window is not minimized and if so, minimize it */
	if (mainWindowState != GDK_WINDOW_STATE_ICONIFIED) {
		gtk_window_iconify(GTK_WINDOW(main_window));
		gtk_window_set_skip_taskbar_hint(GTK_WINDOW(main_window), TRUE);
	}
	/* restore the window and show it on the taskbar */
	else {
		gtk_window_present(GTK_WINDOW(main_window));
		gtk_window_set_skip_taskbar_hint(GTK_WINDOW(main_window), FALSE);
	}
	
}

GdkPixbuf *get_file_icon(const gchar *file) {
	/* load file icon into pixbuf */
	gchar *uri = gnome_vfs_get_uri_from_local_path(file);
	gchar *icon_name = gnome_icon_lookup_sync(gtk_icon_theme_get_default(), NULL, uri, NULL, GNOME_ICON_LOOKUP_FLAGS_NONE, NULL);
	GdkPixbuf *pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), icon_name, ICON_SIZE, 0, NULL);

	g_free(uri);
	g_free(icon_name);
	return pixbuf;
}

gchar *shrink_path_with_tilde(gchar *path) {
	/* this function shrinks a path containing the users home directory by using a ~ */
	const char *home = getenv("HOME");
	
	if (strlen(home) <= strlen(path)) {
		if (strncmp(path, home, strlen(home)) == 0) {
			gchar *file;
			file = g_malloc(strlen(path)-strlen(home)+2);
			file[0] = '~';
			strcpy(&file[1], &path[strlen(home)]);
			g_free(path);
			return file;
		}
	}
	else {
		return path;
	}
}

/* modified from gnome-vfs because their version returns NULL if '#' is in the uri */
char *watchpost_get_local_path_from_uri (const char *uri) {
	const char *path_part;

	if (!_gnome_vfs_istr_has_prefix (uri, "file:/")) {
		return NULL;
	}
	
	path_part = uri + strlen ("file:");
	
	if (_gnome_vfs_istr_has_prefix (path_part, "///")) {
		path_part += 2;
	} else if (_gnome_vfs_istr_has_prefix (path_part, "//")) {
		return NULL;
	}

	return gnome_vfs_unescape_string (path_part, "/");
}
/* taken directly from gnome-vfs */
gboolean
_gnome_vfs_istr_has_prefix (const char *haystack, const char *needle)
{
	const char *h, *n;
	char hc, nc;

	/* Eat one character at a time. */
	h = haystack == NULL ? "" : haystack;
	n = needle == NULL ? "" : needle;
	do {
		if (*n == '\0') {
			return TRUE;
		}
		if (*h == '\0') {
			return FALSE;
		}
		hc = *h++;
		nc = *n++;
		hc = g_ascii_tolower (hc);
		nc = g_ascii_tolower (nc);
	} while (hc == nc);
	return FALSE;
}


